<template>
  <PageTemplate>
    <div class="sri-process" slot="content">
      <el-form
        inline
        :model="query"
        ref="query"
        label-position="right"
        label-width="82px"
        class="search-collapse-pro-form"
      >
        <SearchCollapsePro>
          <div slot="title" class="search-collapse-pro-title">
            <el-form-item label-width="0px">
              <el-input v-model="query.serviceName" :placeholder="$t('pur.service_name')">
                <el-button slot="append" icon="el-icon-search" @click="queryDataTableLike"></el-button>
              </el-input>
            </el-form-item>
          </div>
          <div slot="collapse">
            <!-- 高级搜索具体内容 -->
            <el-form-item :label="$t('pur.process_year')" prop="year">
              <el-input v-model="query.year"></el-input>
            </el-form-item>
            <el-form-item :label="$t('pur.project_name')" prop="projectName">
              <el-input v-model="query.projectName"></el-input>
            </el-form-item>
            <el-form-item :label="$t('pur.process_contactUser')" prop="contactUser">
              <el-input v-model="query.contactUser"></el-input>
            </el-form-item>
            <el-form-item :label="$t('pur.contract_arrival_time_start')" prop="requiredArrivalTimeStart">
              <el-date-picker v-model="query.requiredArrivalTimeStart" type="date" placeholder="选择日期"></el-date-picker>
            </el-form-item>
            <el-form-item :label="$t('pur.contract_arrival_time_end')" prop="requiredArrivalTimeEnd">
              <el-date-picker v-model="query.requiredArrivalTimeEnd" type="date" placeholder="选择日期"></el-date-picker>
            </el-form-item>

            <!-- 查询和重置按钮-->
            <el-row :gutter="10">
              <div class="btn-group-search">
                <el-col :span="2">&nbsp;</el-col>
                <el-col :span="6">
                  <el-button
                    type="primary"
                    class="btn-default"
                    @click="getAllDataTable"
                    size="small"
                  >{{$t('cm.query')}}</el-button>
                  <el-button
                    class="btn-second"
                    @click="queryReset('query')"
                    style="margin:0;"
                    size="small"
                  >{{$t('cm.reset')}}</el-button>
                </el-col>
              </div>
            </el-row>
          </div>
        </SearchCollapsePro>
      </el-form>
      <el-row class="row-empty-20"></el-row>
      <PageTableCollapse :collapseStatus="true">
        <el-row slot="title">
          <!-- <el-col :span="12"><div style="line-height: 40px"> {{$t('sri.table_title')}} </div></el-col> -->
          <el-col>
            <!-- style="float:right;" -->
            <div>
              <el-button type="primary" @click="exportOp">
                <i class="el-icon-download"></i> {{$t('cm.export')}}
              </el-button>
            </div>
          </el-col>
        </el-row>
        <el-table
          class="table"
          border
          ref="multipleTable"
          :data="tables"
          tooltip-effect="dark"
          style="width: 100%"
          :show-overflow-tooltip="true"
          @selection-change="handleSelectionChange"
          :header-cell-style="{background:tableHeaderBGC}"
        >
          <el-table-column align="center" type="selection" width="60"></el-table-column>
          <el-table-column type="index" :label="$t('cm.no')" align="center" width="60"></el-table-column>
          <el-table-column prop="demandCode" :label="$t('pur.requirement_number')" align="center" ></el-table-column>
          <el-table-column prop="year" :label="$t('pur.process_year')" align="center"></el-table-column>
          <el-table-column prop="allocationName" :label="$t('pur.demand_follow')" align="center"></el-table-column>
          <el-table-column prop="procType" :label="$t('pur.withinContract')" align="center" :formatter="formatTableText"></el-table-column>
          <el-table-column prop="contractNo" :label="$t('pur.contract_no')" align="center"></el-table-column>
          <el-table-column prop="contractName" :label="$t('pur.contract_name')" align="center"></el-table-column>
          <el-table-column prop="projectName" :label="$t('pur.project_name')" align="center"></el-table-column>
          <el-table-column prop="serviceName" :label="$t('pur.service_name')" align="center"></el-table-column>
          <el-table-column prop="specification" :label="$t('pur.specification')" align="center"></el-table-column>
          <el-table-column prop="num" :label="$t('pur.contract_item_num')" align="center"></el-table-column>
          <el-table-column prop="unit" :label="$t('pur.contract_item_unit')" align="center"></el-table-column>
          <el-table-column prop="requiredArrivalTime" :label="$t('pur.contract_arrival_time')" align="center"></el-table-column>
          <el-table-column prop="plannedCompletionTime" :label="$t('pur.plannedCompletionTime')" align="center"></el-table-column>
          <el-table-column prop="allocationName" :label="$t('pur.allocation_name')" align="center"></el-table-column>
          <el-table-column prop="createTime" :label="$t('pur.allocation_time')" align="center"></el-table-column>
          <el-table-column prop="contactUser" :label="$t('pur.process_contactUser')" align="center" ></el-table-column>
          <el-table-column prop="mobile" :label="$t('pur.process_mobile')" align="center" ></el-table-column>
          <el-table-column prop="folio" :label="$t('pur.demand_folio')" align="center" ></el-table-column>
          <el-table-column :label="$t('cm.operate')" align="center" fixed="right" width="120">
            <template slot-scope="scope">
              <ListBtn :options="btns" :row-data="scope.row" @triggerListBtn="handleListBtn"></ListBtn>
            </template>
          </el-table-column>
        </el-table>
        <div class="block pager-customer">
          <el-pagination
            background
            layout="total,prev,pager, next,jumper"
            :current-page.sync="pagingCurrentPageNum"
            :page-sizes="pagingPageSizes"
            :page-size="pagingPageSize"
            :total="pagingTotal"
            @size-change="pagingHandleSizeChange"
            @current-change="pagingHandleCurrentChange"
          >
          </el-pagination>
        </div>
      </PageTableCollapse>
    </div>
  </PageTemplate>
</template>
<style lang="scss">
</style>
<script>
import { mapState, mapActions } from 'vuex'
import PageTemplate from '@/components/common/PageTemplate'
import PageTableCollapse from '@/components/common/PageTableCollapse'
import SearchCollapsePro from '@/components/common/SearchCollapsePro'
import ListBtn from '@/components/btn/ListBtn'
import { ALERT } from '@/utils/responseHandle'
import * as Constant from '@/utils/constant'
import {COMMON} from '@/utils/commonUtil'
import Pagination from '@/mixins/Pagination'
import HasBtn from '@/mixins/HasBtn'
import PurTrackDem from './js/PurTrackDem'

export default {
  components: { PageTemplate, PageTableCollapse, SearchCollapsePro, ListBtn },
  data () {
    return {
      query: {
        serviceName: '',
        year: null
      },
      tables:[],
      btns: [
        {
          value: 'info',
          label: 'pur.allocation'
        },
        {
          value: 'edit',
          label: 'pur.disallocation'
        }
      ]
    }
  },
  created () {
    this.getAllDataTable()
  },
  mixins: [Pagination, HasBtn],
  watch: {
  },
  methods: {
    ...mapActions('common', [
      'queryCollapse'
    ]),
    //重置操作
    queryReset (formName) {
      this.$refs[formName].resetFields()
    },
    queryDataTableLike () {
      this.queryTableList({
        ...this.query,
        pageNum: this.pagingCurrentPageNum,
        pageSize: this.pagingPageSize
      })
    },
    getAllDataTable () {
      this.queryTableList({
        ...this.query,
        pageNum: this.pagingCurrentPageNum,
        pageSize: this.pagingPageSize
      })
    },
    //统一与后台操作的方法
    queryTableList (params) {
      //这里的params是对象
      //注意 如果后台json转对象 时间类型好像会抛异常  转到后台 前台用 qs 来转换数据 设置一个全局拦截器 main.js 参考
      let _this = this
      let tables = []
      let totalCount = 0
      PurTrackDem.loadByPaging(params, result => {
        if (result.code === Constant.RT_SUCCESS) {
          //表格数据  数组            
          tables = result.data.results
          //分页数据
          totalCount = result.data.totalRows  
        } else {
          // _this.$t('cm.tips')
          ALERT.error(result.data.msg)
        }
        _this.setPagingTotal(totalCount)
        this.tables = tables
      })
    },
    info (row) {
      COMMON.routerTo(Constant.ROUTE_PUR_TRACK_DEM_ALLOCATION.index, this.$router, { id: row.id })
    },
    edit (row) {
      COMMON.confirm({}, () => {
        PurTrackDem.cancelAllocation({ demandId: row.id }, result => {
          if (result.code === Constant.RT_SUCCESS) {
            ALERT.success(result.msg)
            this.getAllDataTable()
          } else {
            // _this.$t('cm.tips')
            ALERT.error(result.msg)
          }
        });
      })
    },
    exportOp() {

    },
    formatTableText (row, column, cellValue, index) {
      var fmtValue = null
      let procTypeMap = {1:"合同范围", 2:"非合同范围"};
      if (column.property === 'procType') {
        fmtValue = procTypeMap[row.procType]
      }
      return fmtValue !== null && fmtValue !== undefined ? fmtValue : cellValue
    }
  },
  mounted () {
  },
  computed: {
    ...mapState({
      tableHeaderBGC: state => state.common.tableHeaderBGC
    })
  }
}
</script>